package com.sg.zhui.visitor.config;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;



/**
 * Created by Administrator on 2016/11/3.
 */
@SuppressLint({ "WorldReadableFiles", "InlinedApi" })
public class UserConfig_host {

    /**
     * 用share的形式存取配置信息
     * <p>
     * 和账户有关的配置信息，存在这里。 切换用户登录，信息会被清空。 ！！！
     */
    private static final String getNameSetting(Context context)
    {
        String name = context.getPackageName() +"."+"user_config" ; // +"."+"user_config"
        return name;
    }

    // 设置配置值
    @SuppressWarnings("deprecation")
    public static void setConfig(Context context, String name,
                                 Object valueObject) {
        name = getKey(name);
        SharedPreferences shared = context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = shared.edit();
        if (valueObject instanceof String) {
            String value = (String) valueObject;
            editor.putString(name, value);
        } else if (valueObject instanceof Integer) {
            int value = (Integer) valueObject;
            editor.putInt(name, value);
        } else if (valueObject instanceof Float) {
            float value = (Float) valueObject;
            editor.putFloat(name, value);
        } else if (valueObject instanceof Long) {
            long value = (Long) valueObject;
            editor.putLong(name, value);
        } else if (valueObject instanceof Boolean) {
            boolean value = (Boolean) valueObject;
            editor.putBoolean(name, value);
        }
        editor.commit();
    }

    // 获取配置 string
    @SuppressWarnings("deprecation")
    public static String getConfigString(Context context, String name,
                                         String defaultValue) {

        Context app360context = null;
        try {
            app360context = context.createPackageContext(context.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            return defaultValue;
        }
        SharedPreferences share = app360context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);
        return share.getString(getKey(name), defaultValue);
    }

    // 获取配置 int
    @SuppressWarnings("deprecation")
    public static int getConfigInt(Context context, String name,
                                   int defaultValue) {

        Context app360context = null;
        try {
            app360context = context.createPackageContext(context.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            return defaultValue;
        }
        SharedPreferences share = app360context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);
        return share.getInt(getKey(name), defaultValue);
    }

    // 获取配置 boolean
    @SuppressWarnings("deprecation")
    public static boolean getConfigBoolean(Context context, String name,
                                           boolean defaultValue) {
        Context app360context = null;
        try {
            app360context = context.createPackageContext(context.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            return defaultValue;
        }
        SharedPreferences share = app360context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);
        return share.getBoolean(getKey(name), defaultValue);
    }

    // 获取配置long
    @SuppressWarnings("deprecation")
    public static long getConfigLong(Context context, String name,
                                     long defaultValue) {
        Context app360context = null;
        try {
            app360context = context.createPackageContext(context.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            return defaultValue;
        }
        SharedPreferences share = app360context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);

        return share.getLong(getKey(name), defaultValue);
    }

    public static void clearLoginConfig(Context context) {
        setConfig(context, Const.CONFIG_APP_LOGIN_INFO, "");
    }

    @SuppressWarnings("deprecation")
    public static void clearConfig(Context context) {
        Context app360context = null;
        try {
            app360context = context.createPackageContext(context.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            return;
        }
        SharedPreferences share = app360context.getSharedPreferences(
                getNameSetting(context), Context.MODE_PRIVATE
                        + Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = share.edit();
        editor.clear();
        editor.commit();
    }

    public static String getKey(String name) {
        if (name.equals(Const.CONFIG_APP_LOGIN_INFO)) {
            return name;
        }

//        if (LoginUtil_host.checkIsLogin()) {
//            name = MyApplication_host.getInstance().getLoginInfo().userId + "_"
//                    + name;
//        }
        return name;
    }
}
